package mi.payment.utils.transaction;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.ParseUtils;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;

/**
 * 请求二维码数据
 *
 * @author tomcat on 2017/2/17.
 */
public class RequestQRCodeTask extends TransactionTask<TradeData, Void, TradeData> {

    protected RequestQRCodeTask(Context context) {
        super(context, true);
    }

    @Override
    protected void onBegin(TradeData[] params) {
        setDBStorage(true);
        setUpdateSettlementTable(false);
        tradeData = params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        /*
         * A2-订单已支付
         * A3-二维码已过期，请刷新再试
         * A4-不支持卡类型
         * A5-订单已关闭
         * A6-订单已撤销
         * A7-用户支付中，需要输入密码
         * A8-交易失败，请稍后重试
         * A9-退款金额大于支付金额
         * AA-订单未支付
         * AB-订单已关闭
         * AC-订单已撤销
         * AE-订单支付失败
         * AW-用户支付中
         * AF-请更换其他付款方式
         * B1-支付尚未开通，请联系代理商。
         * B2-请一键初始化
         * C1-无效的优惠券
         * C2-无效的交易金额
         * W1-订单异常
         * W2-请扫描正确条码/二维码
         */
        if ("AW".equals(resultStr)) {
            dbBean.setTradeAnswerCode("00");
        }
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_QR_CODE_TRADE);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        dbBean.setProcessCode("660000");
        dbBean.setTransType("22");
        dbBean.setNetManageCode("000");
        dbBean.setTerminalDate(System.currentTimeMillis() / 1000);

        fieldMap = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 通道类型 微信-TX01  盛付通钱包-SFT01  支付宝-ZFB01
        String channelType = "TX01";
        if (dbBean.transPlatform == 1) {
            // 支付宝
            channelType = "ZFB01";
        } else if (dbBean.transPlatform == 2) {
            // 微信
            channelType = "TX01";
        } else if (dbBean.transPlatform == 4) {
            // 微信
            channelType = "CUP01";
        }

        // 填写22域
        MsgField fieldF022 = fieldMap.get("F022");
        fieldF022.setDataStr("031"); // 用法描述 031:反扫 032:正扫
        fieldF022.setDataLength(3);

        // 填写47域
        MsgField fieldF047 = fieldMap.get("F047");
        StringBuilder value47 = new StringBuilder();
        value47.append("TXNWAY=");
        value47.append(channelType);
        fieldF047.setDataStr(value47.toString());
        fieldF047.setDataLength(value47.length());

        // 填写60域
        MsgField fieldF060 = fieldMap.get("F060");
        String value60 = dbBean.getTransType() + dbBean.getBathNo() + dbBean.getNetManageCode();
        fieldF060.setDataStr(value60);
        fieldF060.setDataLength(value60.length());

        // 添加自定义的商户号
        fieldMap = ParseUtils.addCustomTransID(fieldMap);

        return EncodePackage.assembly(fieldMap, "0200", TerminalSecure.PROCESSREQUEST_NONE);
    }

    @Override
    protected void onUnpacked(HashMap<String, MsgField> resp) {
        // 请求到的二维码数据
        // 返回示例：47 = SCANPAYURL=weixin://wxpay/bizpayurl?pr=C1DN1rp
        MsgField f047 = resp.get("F047");
        if (f047 != null) {
            String s = f047.getDataStr().substring(11);
            dbBean.setPayQRCode(s);
        }
    }

}
